//
//  PlotterV.m
//  eyr
//
//  Created by Tor Gjøsæter on 5/1/12.
//  Copyright (c) 2012 University of Bergen. All rights reserved.
//

#import "PlotterV.h"
#import <QuartzCore/CAAnimation.h>

    
@implementation PlotterV
@synthesize value;
@synthesize lastPlot;
@synthesize values;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    
    return self;
}

- (void) plot:(int)v {
    if ([self.values count] == 0) {
        values = [[NSMutableArray alloc] initWithCapacity:512];
        for (int i = 0; i<512; i++) {
            [self.values addObject:[[NSNumber alloc] initWithFloat:0.0]];
        }
    }
    [self.values removeObjectAtIndex:0];
    [self.values addObject:[[NSNumber alloc] initWithFloat:v]];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    //teikner ei linje bortober
    //[self drawOld];
    
    //Teikner som i processing
    [self drawLikeProcessing];
    
    //deler inn i uker
    
}


- (void) drawLikeProcessing{
    
    //Denne teikner heile viewen på nytt
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    CGPoint midPoint;
    midPoint.x = self.frame.size.width  / 2;
    midPoint.y = self.frame.size.height / 2;
    
    UIGraphicsPushContext(context);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, midPoint.y);

    float opaopa = 0;
    
    for (int x=0; x<[self.values count]; x++) {
        NSNumber *y = [self.values objectAtIndex:x];
  
        opaopa = [y floatValue];
        CGPoint top;
        top.x = x;
        top.y =  midPoint.y - [y floatValue];
    
        CGPoint bottom;
        bottom.x = x;
        bottom.y = midPoint.y + [y floatValue];
        
        
        CGContextAddLineToPoint(context, top.x, top.y);
        CGContextAddLineToPoint(context, bottom.x, bottom.y);
        
 
        
        
    }
    
    
    [[UIColor blackColor] setStroke];

    //slå denne av og på for stilige effekter
    CGContextSetShouldAntialias(context, NO);
    
    
    CGContextDrawPath(context, kCGPathStroke);   
    UIGraphicsPopContext();

    
}

- (void) drawOld{    //teikner ei linje bortober

    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    CGPoint midPoint;
    midPoint.x = self.frame.size.width  / 2;
    midPoint.y = self.frame.size.height / 2;
    
    UIGraphicsPushContext(context);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0, midPoint.y);
    for (int x=0; x<[self.values count]; x++) {
        NSNumber *y = [self.values objectAtIndex:x];
        CGContextAddLineToPoint(context, x, midPoint.y - [y floatValue]);
    }
    [[UIColor blackColor] setStroke];
    
    CGContextSetLineWidth(context, 3.0);
    CGContextDrawPath(context, kCGPathStroke);   
    UIGraphicsPopContext();

}


- (void)drawMonth{
    NSString *month = @"Januar";
    CGRect monthrect = CGRectMake(0, 0, 1024, 768);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor grayColor] setFill];   
    CGContextAddRect(context, monthrect);   
    CGContextFillRect(context, monthrect);
  
    UIFont *font = [UIFont boldSystemFontOfSize:23];
    CGRect textRect;
    textRect.size = [month sizeWithFont:font];
    textRect.origin.x = 0;
    textRect.origin.y = 0;
    [[UIColor blackColor] setFill];   
    [month drawInRect:textRect withFont:font];
    
    [self drawWeek];
    
}

- (void)drawWeek
{   
    float posy = 30;
    CGRect weekrect = CGRectMake(0, posy, 1024, 768/7);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor purpleColor] setFill];   
    CGContextAddRect(context, weekrect);   
    CGContextFillRect(context, weekrect);
    
    
    [self drawDay];
    
    NSString* week = @"Uke";
    UIFont *font = [UIFont boldSystemFontOfSize:23];
    CGRect textRect;
    textRect.size = [week sizeWithFont:font];
    textRect.origin.x = 0;
    textRect.origin.y = posy;
    [[UIColor blackColor] setFill];
    CGContextRotateCTM(context, 90);

    [week drawInRect:textRect withFont:font];
    
}

- (void)drawDay{
    float posy = 30;
    CGRect dayrect = CGRectMake(0, posy, 1024/7, 768/7);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor purpleColor] setFill];
    [[UIColor yellowColor] setStroke];
    CGContextAddRect(context, dayrect);   
    CGContextFillRect(context, dayrect);

    
    NSString* day = @"Dag";
    UIFont *font = [UIFont boldSystemFontOfSize:23];
    CGRect textRect;
    textRect.size = [day sizeWithFont:font];
    textRect.origin.x = 0;
    textRect.origin.y = 0;
    [[UIColor blackColor] setFill];
    
    [day drawInRect:textRect withFont:font];
}


                

@end
